<?php
/**
*@fileOverview:Wordpad Forum
*@author:<a href= "www.rcorp.co.in>Ramanan Corporation</a>
*@version: v1.0
*/
require_once('connection.php');
/**
*@class The Class for Forum of Wormpad.
*@description The Class for Forum of Wormpad.This class contains data memebers and methods for operations on threads.
*@name Forum
*@requires Requires connection.php for establishing connection to the database.
*/
class Forum
{
	/**
	*@name threadId_
	*@type Integer
	*@description Refers to the Id of the Thread
	*@memberOf Forum
	*@private
	*/
	private $threadId_;		
	/**
	*@name userId_
	*@description Refers to the Id of the User
	*@type Integer
	*@memberOf Forum
	*@private
	*/
	private $userId_;
	/**
	*@name threadCategory_
	*@description Refers to the Category of the Thread
	*@type String
	*@memberOf Forum
	*@private
	*/
	private $threadCategory_;
	/**
	*@name threadSubject_
	*@description Refers to Subject of the Thread
	*@type String
	*@memberOf Forum
	*@private
	*/
	private $threadSubject_;
	/**
	*@name isVisible_
	*@description Refers to the Visibility of the Thread
	*@type Boolean
	*@memberOf Forum
	*@private
	*/
	private $isVisible_;
	/** 
	*@name timestamp_
	*@description Refers to the date of creation of the Thread
	*@type Timestamp
	*@memberOf Forum
	*@private
	*/
	private $timestamp_;
	
	
	/**
	*@description Function getThreadId() is used to get Thread Id of the Thread
	*@return {Integer} The function returns Id of the Thread
	*@memberOf Forum
	*/
	function getThreadId()
	{
		return $this->threadId_;	
	}
	/**
	*@description Function getUserId() is used to get User Id of the User
	*@return {Integer} The function returns Id of the User
	*@memberOf Forum
	*/
	function getUserId()
	{
		return $this->userId_;	
	}
	/**
	*@description Function getThreadCategory() is used to get Cateogry of the Thread
	*@return {String} The function returns Category of the Thread
	*@memberOf Forum
	*/
	function getThreadCategory()
	{
		return $this->threadCategory_;	
	}
	/**
	*@description Function getThreadSubject() is used to get the Subject of the Thread
	*@return {String} The function returns Subject of the Thread
	*@memberOf Forum
	*/
	function getThreadSubject()
	{
		return $this->threadSubject_;
	}
	/**
	*@description Function getIsVisible() is used to get the Visibility Status of the Thread
	*@return {Boolean} The function returns visibility of the Thread
	*@memberOf Forum
	*/
	function getIsVisible()
	{
		return $this->isVisible_;	
	}
	/**
	*@description Function getTimestamp() is used to get Date of Creation of the Thread
	*@return {Timestamp} The function returns Date of Creation of the Thread
	*@memberOf Forum
	*/
	function getTimestamp()
	{
		return $this->timestamp_;	
	}
	
	
	
	/**
	*@description Function setThreadId() is used to set Thread Id of the Thread
	*@param {Integer} threadId The Function accepts threadId, which is the Id of the Thread
	*@memberOf Forum
	*/
	function setThreadId($threadId)
	{
		$this->threadId_ = $threadId;	
	}
	/**
	*@description Function setUserId() is used to set User Id of the User who has created the Thread
	*@param {Integer} userId The Function accepts userId, which is the Id of the User
	*@memberOf Forum
	*/
	function setUserId($userId)
	{
		$this->userId_ = $userId;	
	}
	/**
	*@description Function setThreadCategory() is used to set Cateogry of the Thread
	*@param {String} threadCategory The Function accepts threadCategory, which is the Category of the Thread
	*@memberOf Forum
	*/
	function setThreadCategory($threadCategory)
	{
		$this->threadCategory_ = $threadCategory;	
	}
	/**
	*@description Function setThreadSubject() is used to set the Subject of the Thread
	*@param {String} threadSubject The Function accepts threadSubject, which is the Subject of the Thread
	*@memberOf Forum
	*/
	function setThreadSubject($threadSubject)
	{
		$this->threadSubject_ = $threadSubject;
	}
	/**
	*@description Function setIsVisible() is used to set Visibilty of the Thread
	*@param {Boolean} isVisible The Function accepts isVisible, which is the visibility of the Thread
	*@memberOf Forum
	*/
	function setIsVisible($isVisible)
	{
		$this->isVisible_ = $isVisible;	
	}
	/**
	*@description Function setTimestamp() is used to set Date of Creation of the Thread
	*@param {Timestamp} timestamp The Function accepts timestamp, which is the Date of Creation of the Thread
	*@memberOf Forum
	*/
	function setTimestamp($timestamp)
	{
		$this->timestamp_ = $timestamp;	
	}
	
	
	/**
	*@description Function loadByCategory() loads Threads which falls under a specified category
	*@param {String} category The Function accepts category, which is the category of the Thread
	*@return {Forum[]} The Function returns Array of objects of Forum
	*@memberOf Forum
	*/
	function loadByCategory($category)
	{
		$threads=array();
		$query="SELECT * FROM thread_details WHERE thread_category='".$category."'";
		$result=mysql_query($query);
		while($row=mysql_fetch_array($result,MYSQL_ASSOC))
		{
			$thread=new Forum();
			$post=new Post();
			$thread->threadId_ = $row['thread_id'];
			$thread->userId_ = $row['user_id'];
			$thread->threadCategory_ = $row['thread_category'];	
			$thread->threadSubject_ = $row['thread_subject'];
			$thread->isVisible_ = $row['isVisible'];
			$thread->timestamp_ = $row['timestamp'];
			$threads[]=$thread;
		}
		return $threads;
	}
	
	/**
	*@description Function loadByThreadId() loads Thread which has the specified ThreadId
	*@param {Integer} threadId The Function accepts Id, which is the Id of the Thread
	*@memberOf Forum
	*/	
	function loadByThreadId($threadId)
	{
		$query="SELECT * FROM thread_details WHERE thread_id='".$threadId."'";
		$result=mysql_query($query);
		$row=mysql_fetch_array($result,MYSQL_ASSOC);
		$post=new Post();
		$this->threadId_ = $row['thread_id'];
		$this->userId_ = $row['user_id'];
		$this->threadCategory_ = $row['thread_category'];	
		$this->threadSubject_ = $row['thread_subject'];
		$this->isVisible_ = $row['isVisible'];
		$this->timestamp_ = $row['timestamp'];
	}
	
	/**
	*@description Function loadBySubject() loads Threads which has a similar subject to the specifed String
	*@param {String} subject The Function accepts Subject, which is the Subject of the Thread
	*@return {Forum[]} The Function returns Array of objects of Forum
	*@memberOf Forum
	*/
	function loadBySubject($subject)
	{
		$threads=array();
		$query="SELECT * FROM thread_details WHERE thread_subject LIKE '%".$subject."%'";
		$result=mysql_query($query);
		while($row=mysql_fetch_array($result,MYSQL_ASSOC))
		{
			$thread=new Forum();
			$post=new Post();
			$thread->threadId_ = $row['thread_id'];
			$thread->userId_ = $row['user_id'];
			$thread->threadCategory_ = $row['thread_category'];	
			$thread->threadSubject_ = $row['thread_subject'];
			$thread->isVisible_ = $row['isVisible'];
			$thread->timestamp_ = $row['timestamp'];
			$threads[]=$thread;
		}
		return $threads;
	}
	
	/**
	*@description Function save() adds a new Thread to the database. The values are to be set using set functions before calling the save() function
	*@return {Integer} The Function returns the ThreadId of the inserted Thread
	*@memberOf Forum
	*/
	function save()
	{
		$query="INSERT INTO thread_details
		(
			user_id,
			thread_category,
			thread_subject,
			isVisible
		)
		VALUES
		(
			'".$this->userId_."',
			'".$this->threadCategory_."',
			'".$this->threadSubject_."',
			true
		)";
		
		mysql_query($query);
		return mysql_insert_id();
	}
/* 
 * ==============================================================================================================================
 * CHANGES/MODIFICATION/ADDTION BY ABHINEET STARTS	/////////////////////////////////////////////////////////////////////////////
 * ==============================================================================================================================
 */
	function getUsername($user_id)
	{
		$query="SELECT user_firstname FROM login_details WHERE user_id='".$user_id."'";
		$result=mysql_query($query);
		$row=mysql_fetch_array($result,MYSQL_ASSOC);
		$username=$row['user_firstname'];
		return $username;
	}
	function totalPost($thread_id)
	{
		$query="SELECT * FROM post_details WHERE thread_id='".$thread_id."';";
		$result=mysql_query($query);
		//$row=mysql_fetch_array($result,MYSQL_ASSOC);
		while($row=mysql_fetch_array($result,MYSQL_ASSOC))
		{
			$count[]=$row['post_id'];
			//$count[]=$row['user_id'];
		}
		return count($count);
	}
	
	function searchByCategory($keyword,$category)
	{
		$query="SELECT * FROM thread_details WHERE thread_subject LIKE '%".$keyword."%' OR thread_category LIKE '%".$category."%'";
		$result=mysql_query($query);
		$searchResult=array();
		while($row=mysql_fetch_array($result,MYSQL_ASSOC))
		{
			$thread = new Forum();
			$post = new Post();
			$thread->threadId_ = $row['thread_id'];
			$thread->userId_ = $row['user_id'];
			$thread->threadCategory_ = $row['thread_category'];	
			$thread->threadSubject_ = $row['thread_subject'];
			$thread->isVisible_ = $row['isVisible'];
			$thread->timestamp_ = $row['timestamp'];
			$searchResult[]=$thread;
		}
		return $searchResult;
	}
 
/* 
 * ==============================================================================================================================
 * CHANGES/MODIFICATION/ADDTION BY ABHINEET ENDS	/////////////////////////////////////////////////////////////////////////////
 * ==============================================================================================================================
 */
}




?>